package de.uni.bremen.estudent.lbs.ui.phone;


import android.app.backup.BackupManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import de.uni.bremen.estudent.lbs.R;
import de.uni.bremen.estudent.lbs.util.CategoryListAdapter;
import de.uni.bremen.estudent.lbs.util.CategoryListAdapter.ViewHolder;
import de.uni.bremen.estudent.lbs.util.Constants;

import java.util.HashMap;
import java.util.Map;


/**
 * Class represents the filter dialog.
 * 
 * @author bahlfeld
 * @version 1.0 05.04.2012
 */
public final class FilterDialog extends FragmentActivity {

    /**
     * Saves the selected categories in a map.
     */
    private Map<String, Boolean> categoryIsEnabled;

    /**
     * Event method for the cancle button in activity_filterdialog.
     * 
     * @param v
     */
    public void onCancel(final View v) {
        finish();
    }

    /**
     * Event method for the checkboxes in the fragment_listcategory. Saves the
     * filtered categories in a list.
     * 
     * @param view
     */
    public void onCategoryListItemClick(final View view) {
        CategoryListAdapter.ViewHolder vh = (ViewHolder) view.getTag();
        if (vh != null) {
            categoryIsEnabled.put(vh.categoryCheckBox.getText().toString(),
                                  vh.categoryCheckBox.isChecked());
        }
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filterdialog);
        categoryIsEnabled = new HashMap<String, Boolean>();
    }

    /**
     * Event method for the filter button in activity_filterdialog. Writes the
     * filtered categories in the sharedpreferences.
     * 
     * @param v
     */
    public void onOk(final View v) {
        SharedPreferences prefs = getSharedPreferences(Constants.PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        for (String key : categoryIsEnabled.keySet()) {
            editor.putBoolean(key, categoryIsEnabled.get(key));
        }
        editor.commit();
        BackupManager bManager = new BackupManager(getApplicationContext());
        bManager.dataChanged();
        this.finish();
    }
}
